from threading import *

import xml.dom.minidom
from xml.dom.minidom import Node, parseString

class Multiplexer(Thread):
	def __init__(self, queue):
		self.lck = Lock()
		self.listeners = []
		self.queue = queue
		Thread.__init__(self)

	def addListener(self, l):
		self.lck.acquire()
		self.listeners.append(l)
		self.lck.release()

	def dispatchListeners(self, node):
		self.lck.acquire()
		for i in self.listeners:
			if i.name == node.nodeName:
				i.notify(node)
		self.lck.release()

	def run(self):
		while True:
			str = self.queue.get()
			doc = parseString(str)

			root = doc.documentElement
			elem = doc.childNodes[0]
			self.dispatchListeners(elem)
